//导入mysql模块
var mysql = require('mysql');
var dbconfig = require('./dbConfig');
var integralSql = require('./scoresql');
var Code = require('./checkCode');
//使用dbconfig.js 配置创建一个mysql连接池
var pool = mysql.createPool(dbconfig.mysql);


//查 query
function getScoreQueryALL(callback){
    pool.getConnection(function(err,connection){
        //建立连接
        connection.query(integralSql.queryAll,function(err,result){
            connection.release();
            callback(Code(result,err));
        })
    })
}

function getScoreByPhone(phone,callback){
    pool.getConnection(function(err,connection){
        if(phone==undefined)return;
        connection.query(integralSql.getScoreByPhone,phone,function(err,result){
            connection.release();
            callback(Code(result,err));
        })
    })
}
//增 insert
function addScoreTable(phone,callback){
    pool.getConnection(function(err,connection){
        //建立连接
        connection.query(integralSql.insert,phone,function(err,result){
            connection.release();
            callback(Code(result,err));
        })
    })
};
//删 delete
function delScoreTable(phone,callback){
    pool.getConnection(function(err,connection){
        //建立连接
        connection.query(integralSql.delScoreByPhone,phone,function(err,result){
            connection.release();
            callback(Code(result,err));
        })
    })
};

//改 update

function updateScore(params,callback){
    pool.getConnection(function(err,connection){
        //处理数据
        if(params.phone==undefined || params.score==undefined)return;
        //建立连接
        connection.query(integralSql.updateScorePhone,[params.score,params.phone],function(err,result){
            connection.release();
            callback(Code(result,err));
        })
    })
}

module.exports = {getScoreQueryALL,getScoreByPhone,addScoreTable,delScoreTable,updateScore};